package com.itany.mybatis.dao;

import com.itany.mybatis.entity.Hero;
import com.itany.mybatis.entity.HeroType;
import com.itany.mybatis.sql.SelectHeroSql1;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Description: 类的描述
 * Author: liudaolin@itany.com
 * Date: 2021/07/21 09:02
 * Version: 1.0 版本信息
 */
public interface HeroMapper {
    @Insert("insert into t_hero (name, typeId) values (#{name}, #{typeId})")
    void insert(Hero hero);

    @Update("update t_hero set name = #{name} where id = #{id}")
    void updateById(Hero hero);

    @Delete("delete from t_hero where id = #{id}")
    void deleteById(@Param("id") Integer id);

    // 使用默认的方式进行自动映射
    @Select("select id, name, typeId from t_hero")
    List<Hero> selectAll1();

    // 使用 注解Results 手动映射
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "heroName", property = "name"),
            @Result(column = "typeId", property = "typeId")
    })
    @Select("select id, name heroName, typeId from t_hero")
    List<Hero> selectAll2();

    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "typeId", property = "typeId"),
            // 映射1
//            @Result(column = "tid", property = "heroType.id"),
//            @Result(column = "tname", property = "heroType.name"),

            // 关系映射
            @Result(property = "heroType", javaType = HeroType.class,
                    // mapper 类中的 方法的 完全限定名
                    one = @One(select = "selectHeroTypeByTypeId"),
//                    one = @One(select = "com.itany.mybatis.dao.HeroMapper.selectHeroTypeByTypeId"),
                    column = "typeId"
            )
    })
    @Select(" select " +
            "    h.id, h.name, h.typeId, " +
            "    t.id tid, t.name tname " +
            " from t_hero h " +
            " inner join t_hero_type t on h.typeId = t.id ")
    List<Hero> selectAll3();

    @Select("select id, name from t_hero_type where id = #{typeId}")
    HeroType selectHeroTypeByTypeId(@Param("typeId") Integer typeId);


    @SelectProvider(
            type = SelectHeroSql1.class,
            method = "getSql"
    )
    List<Hero> selectByCondition(Hero hero);
}
